package block

import (
	"errors"

	"github.com/golang/protobuf/proto"
	"github.com/hyperledger/fabric-protos-go/common"
	"github.com/hyperledger/fabric-protos-go/peer"
)

func GetTransactionEnvelopeFromPayload(data []byte) (*peer.Transaction, error) {
	var err error
	env := &peer.Transaction{}
	if err = proto.Unmarshal(data, env); err != nil {
		return nil, errors.New("error unmarshaling Envelope")
	}
	return env, nil
}

func GetConfigEnvelopeFromPayload(data []byte) (*common.ConfigEnvelope, error) {
	var err error
	env := &common.ConfigEnvelope{}
	if err = proto.Unmarshal(data, env); err != nil {
		return nil, errors.New("error unmarshaling Envelope")
	}

	return env, nil
}

func GetEnvelopeFromBlock(data []byte) (*common.Envelope, error) {
	var err error
	env := &common.Envelope{}
	if err = proto.Unmarshal(data, env); err != nil {
		return nil, errors.New("error unmarshaling Envelope")
	}

	return env, nil
}
